Skip to content

Method: static {...}

1: package de.fhdw.wtf.generator.java.generatorModel;
2:
3: import java.util.HashMap;
4: import java.util.Map;
5: import java.util.Vector;
6:
7: import de.fhdw.wtf.generator.java.visitor.GenInterfaceClassVisitor;
8: import de.fhdw.wtf.generator.java.visitor.GenInterfaceClassVisitorException;
9: import de.fhdw.wtf.generator.java.visitor.GenInterfaceClassVisitorReturn;
10:
11: /**
12: * Represents an external interface. Such an interface may not be changed by e.g. adding operations to it. It is used to
13: * link generated classes/interfaces via implements/extends to externally available interfaces. This is used e.g. to
14: * make the generated.sums.Anything interface an extension of de.fhdw.wtf.context.model.Anything.
15: *
16: * The class features a static getInstance() method which does a bit of bookkeeping in order to create at most one
17: * GenExternalInterfaceClass object per external interface.
18: */
19: public class GenExternalInterfaceClass extends GenInterfaceClass {
20:         
21:         /**
22:          * Maps fully qualified names of external interfaces to GenExternalInterfaceClass objects.
23:          */
24:         private static Map<String, GenExternalInterfaceClass> extIfaceMap = new HashMap<>();
25:         
26:         /**
27:          * Creates a GenExternalInterfaceClass object.
28:          *
29:          * @param packageName
30:          * The package name of the external interface.
31:          * @param typeName
32:          * The type name of the external interface.
33:          */
34:         protected GenExternalInterfaceClass(final String packageName, final String typeName) {
35:                 super(typeName, new Vector<GenJavaOperation>(), new Vector<GenInterfaceClass>(), GenUnqualifiedPackage
36:                                 .create(packageName), null, null);
37:         }
38:         
39:         /**
40:          * Returns a GenExternalInterfaceClass object for a given external interfaces, specified by a fully qualified name.
41:          *
42:          * @param fullyQualifiedName
43:          * The fully qualified name of an external interface.
44:          * @return The GenExternalInterfaceClass object.
45:          */
46:         public static synchronized GenExternalInterfaceClass getInstance(final String fullyQualifiedName) {
47:                 GenExternalInterfaceClass iface = extIfaceMap.get(fullyQualifiedName);
48:                 if (iface == null) {
49:                         final int pos = fullyQualifiedName.lastIndexOf('.');
50:                         if (pos < 0) {
51:                                 iface = new GenExternalInterfaceClass("", fullyQualifiedName);
52:                         } else {
53:                                 iface =
54:                                                 new GenExternalInterfaceClass(fullyQualifiedName.substring(0, pos),
55:                                                                 fullyQualifiedName.substring(pos + 1));
56:                         }
57:                         
58:                         extIfaceMap.put(fullyQualifiedName, iface);
59:                 }
60:                 return iface;
61:         }
62:         
63:         @Override
64:         public void accept(final GenInterfaceClassVisitor visitor) {
65:                 visitor.handle(this);
66:         }
67:         
68:         @Override
69:         public <X> X accept(final GenInterfaceClassVisitorReturn<X> visitor) {
70:                 return visitor.handle(this);
71:         }
72:         
73:         @Override
74:         public <Y extends Exception> void accept(final GenInterfaceClassVisitorException<Y> visitor) throws Y {
75:                 visitor.handle(this);
76:         }
77:         
78: }